import { getNoneFinish, getFinishWork, selectMyPmById, getWorkTaskCount, getNotHanderWorkByPage, getHanderWorkByPage } from "../api/manage"
import { getUserInfoByUserDDId } from "../api/index"
// import { timeStamp } from "console";
Page({
  data: {
    userDDId: '',
    showState: false,
    toast1Show: false,
    noneFinishList: [],
    FinishWorkList: [],
    userInfo: {},
    tag: 0,
    type: 0,
    currentTab: 1,
    tabs: [
      {
        title: "待办任务",
        content: "待办任务",
      },
      {
        title: "已办任务",
        content: "已办任务",
      },
    ],

    // 待办任务总数量
    waitWorkTaskCount: 0,
    // 已办任务总数量
    completeWorkTaskCount: 0,

    // 待办任务分页查询参数
    notHanderWorkPageParam: {
      page: 1,
      size: 20,
    },
    // 已办任务分页查询参数
    handerWorkPageParam: {
      page: 1,
      size: 20,
    },

    // 待办任务容器加载标识
    isNotHanderTaskContainerLoading: false,
    // 已办任务容器加载标识
    isHanderTaskContainerLoading: false,


  },
  onLoad(query) {
    this.setData({
      tag: query.tag,
      type: Number(query.type),
      currentTab: Number(query.type)
    })
    // this.getNewLoading()

    // 获取当前用户信息
    const res = dd.getStorageSync({
      key: 'currentCity',
    });
    this.setData({
      userDDId: res.data.value.userId
    })

    // 查询数据
    this.getNotHanderWorkByPageFunc()
    this.getHanderWorkByPageFunc()
    this.getWorkTaskCountFunc()
  },

  onPullDownRefresh() {
    this.getNewLoading()
    // 设置加载时间
    setTimeout(() => {
      dd.stopPullDownRefresh()
    }, 5000)
  },

  // 查询待办任务和已办任务的数量
  getWorkTaskCountFunc() {
    getWorkTaskCount({
      userDDId: this.data.userDDId
    }).then((res) => {

      this.setData({
        waitWorkTaskCount: res.data.waitWorkTaskCount,
        completeWorkTaskCount: res.data.completeWorkTaskCount
      })
      // this.data.waitWorkTaskCount = res.data.waitWorkTaskCount
      // this.data.completeWorkTaskCount = res.data.completeWorkTaskCount
    })
  },

  // 分页查询待办任务
  getNotHanderWorkByPageFunc() {

    dd.showLoading({
      content: "加载中"
    })

    let data = {
      page: this.data.notHanderWorkPageParam.page,
      size: this.data.notHanderWorkPageParam.size,
      userDDId: this.data.userDDId
    }

    console.log('#分页查询待办任务', data);

    getNotHanderWorkByPage(data).then((res) => {
      console.log(res.data);

      // 判断是否是第一页
      if (data.page == 1) {
        this.setData({
          noneFinishList: res.data
        })
      } else {
        this.setData({
          noneFinishList: this.data.noneFinishList.concat(res.data)
        })
      }

    }).finally(() => {
      dd.hideLoading()
    })

  },


  onReachBottom() {
    console.log('到底');

    // 判断当前是待办还是已办
    if (this.data.currentTab == 0) {
      this.setData({
        notHanderWorkPageParam: {
          page: this.data.notHanderWorkPageParam.page + 1,
          size: 20
        }
      })
      this.getNotHanderWorkByPageFunc()
    } else {
      this.setData({
        handerWorkPageParam: {
          page: this.data.handerWorkPageParam.page + 1,
          size: 20
        }
      })
      this.getHanderWorkByPageFunc()
    }
  },

  // 分页查询已办任务
  getHanderWorkByPageFunc() {
    dd.showLoading({
      content: "加载中"
    })

    let data = {
      page: this.data.handerWorkPageParam.page,
      size: this.data.handerWorkPageParam.size,
      userDDId: this.data.userDDId
    }

    getHanderWorkByPage(data).then((res) => {
      console.log(res.data);

      // 判断是否是第一页
      if (data.page == 1) {
        this.setData({
          FinishWorkList: res.data
        })
      } else {
        this.setData({
          FinishWorkList: this.data.FinishWorkList.concat(res.data)
        })
      }
    }).finally(() => {
      dd.hideLoading()
    })
  },

  // 查询加载最新数据
  getNewLoading() {
    dd.getStorage({
      key: 'currentCity',
      success: (res) => {
        console.log('本地res', res);
        this.setData({
          userInfo: res.data.value
        })
        let data = {
          userDDId: res.data.value.userId
        }
        getNoneFinish(data).then(res => {
          this.setData({
            noneFinishList: res.data.reverse()
          })
        })
        getFinishWork(data).then(res => {
          this.setData({
            FinishWorkList: res.data.reverse()
          })
        })
      },
      fail: function (res) {
        dd.alert({ content: '请稍后再试' });
      }
    })
  },

  // tabs切换change事件
  tabsChange(e) {
    this.setData({
      currentTab: e
    })
  },

  // 任务点击事件
  taskClick(e) {

    let eItem = e.target.dataset.index
    console.log(eItem);
    let gData1 = {
      pmId: eItem.pmId
    }
    let gData2 = {
      userDDId: ''
    }
    let navParamData = {
      pmId: eItem.pmId,
      userName: "",
      avatar: "",
      pmDeptName: ""
    }

    // 根据pmId查询绩效信息
    selectMyPmById(gData1).then((res) => {
      console.log('查询绩效信息', res);
      gData2.userDDId = res.data.pmUserDDId
      navParamData.pmDeptName = res.data.pmDeptName

      // 根据该绩效的归属者userDDId查询用户信息
      getUserInfoByUserDDId(gData2).then((res1) => {
        console.log('该绩效的用户信息', res1);
        navParamData.userName = res1.data.userName
        navParamData.avatar = res1.data.avatar

        // 进行跳转页面
        dd.navigateTo({
          url: `/pages/usual/performancedetail/performancedetail?pmid=${navParamData.pmId}&userName=${navParamData.userName
            }&avatar=${navParamData.avatar}&dept=${navParamData.pmDeptName}&userId=${res1.data.userId}&managerUserId=${res1.data.managerUserDDId}`,
        })
      })
    })





    // let gData = {
    //   userDDId: eItem.userDDId
    // }
    // getUserInfoByUserDDId(gData).then((res) => {
    //   console.log('该绩效的用户信息', res.data);
    //   eItem.userName = res.data.userName
    //   eItem.avatar = res.data.avatar
    //   eItem.pmDeptName = ''


    // })
  }
});
